package com.preskool.service.impl;

import com.preskool.mapper.SportsMapper;
import com.preskool.po.Sports;
import com.preskool.po.SportsExample;
import com.preskool.service.SportsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: lihao
 * @email: 2470849570@qq.com
 * @date: 20212021/1/8
 * @describe: SportService业务实现层
 * @version: v1.0
 */
@Slf4j
@Service
public class SportServiceImpl implements SportsService {

    @Autowired
    private SportsMapper sportsMapper;

    /**
     * 分页查询
     *
     * @param currentPage   :当前页页码
     * @param pageSize:     页大小
     * @param sortName:排序名称
     * @return
     */
    @Override
    public List<Sports> findAllSports(Integer currentPage, Integer pageSize, String sortName) {
        //创建查询模板
        SportsExample example = new SportsExample();
        currentPage = (currentPage - 1) * pageSize;
        example.setOffset(currentPage);
        example.setLimit(pageSize);
        //通过sortName排序
        example.setOrderByClause(sortName);
        List<Sports> list = sportsMapper.selectByExample(example);
        for (Sports sports : list) {
            log.info("********" + sports);
        }
        return list;
    }

    /**
     * 添加运动类型的操作
     *
     * @param sports
     * @return
     */
    @Override
    public int addSport(Sports sports) {
        //选择性插入数据
        int i = sportsMapper.insertSelective(sports);
        return i;
    }

    /**
     * 通过id删除运动名称
     *
     * @param id
     * @return
     */
    @Override
    public int deleteSport(Integer id) {
        Sports sports = sportsMapper.selectByPrimaryKey(id);
        //判断查询出来的数据是否存在
        if (sports != null) {
            int i = sportsMapper.deleteByPrimaryKey(id);
            return i;
        } else {
            return -1;
        }
    }

    /**
     * 更新运动名称
     *
     * @param sports
     * @return
     */
    @Override
    public int updateSport(Sports sports) {
        Sports sp = sportsMapper.selectByPrimaryKey(sports.getSpid());
        if (sp != null) {
            int i = sportsMapper.updateByPrimaryKeySelective(sports);
            return i;
        }else {
            return -1 ;
        }
    }
}
